using System;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.SmallBasic.Library;

namespace SmallBasicFun.Quizes
{
	public class BubbleQuiz : IQuiz
	{
		private  int called;
		private  int length;
		private  int width;
		private  int xTop;
		private  int yTop;
		private  Primitive paddle;
		private  Primitive paddleY;
		private  int ballX;

		public  Question Question1Move
		{
			get { return Quiz.questions[0]; }
		}

		public  Question Question2SetAction1
		{
			get { return Quiz.questions[1]; }
		}

		public  Question Question3Timer
		{
			get { return Quiz.questions[2]; }
		}
		
		public  Question Question4SetAction2
		{
			get { return Quiz.questions[3]; }
		}

		

		public  Question Question5Ball
		{
			get { return Quiz.questions[4]; }
		}

		


		public  void StartTest()
		{
			GradeResults();
			InitializeScreen();
			DrawSuccess();
			Quiz.DisplayScores(5, 350);
		}

		private  void DrawSuccess()
		{
			DrawBoard();
			CreatePaddle();
			Question2SetAction1.Run();
			Question4SetAction2.Run();
			Question5Ball.Run();
			ballX = xTop + width/2;
			ShapeMaker.CenterShapeAt(Question1Move.GetVariable("ball"), ballX, yTop + length/2);

			6.Times((i) => { called = 156; Question1Move.Run(); });
			called = 0;
			Question3Timer.Run();
		}

		private  void CreatePaddle()
		{
			ShapeMaker.SetColorForNextShape(Colors.Purple);
			paddle = ShapeMaker.CreateRectangle(10, 40);
			paddleY = yTop;
			ShapeMaker.CenterShapeAt(paddle, xTop + width, paddleY);
		}

		private  void DrawBoard()
		{
			GraphicsWindow.PenColor = Colors.Purple;
			GraphicsWindow.PenWidth = 6;
			yTop = 50;
			xTop = 50;
			length = 100;
			width = 100;
			GraphicsWindow.DrawLine(xTop, yTop, xTop + width, yTop);
			GraphicsWindow.DrawLine(xTop, yTop, xTop, yTop + length);
			GraphicsWindow.DrawLine(xTop, yTop + length, xTop + width, yTop + length);
		}


		private  void InitializeScreen()
		{
			ProgramWindow.Clear();
			ProgramWindow.SetBackgroundColor(Colors.Black);
		}


		private  void GradeResults()
		{
			Grade1Move();
			Grade2SetAction1();
			Grade4SetAction2();
			Grade3Timer();
			Grade5Ball();
		}

		private  void Grade5Ball()
		{
			Question5Ball.Run();
			Shape s = ShapeMaker.shapes.GetShape(Question1Move.GetVariable("ball"));
			if(s == null)
			{
				Question5Ball.Result = false;
				return;
			}
			Color expectedColor = GraphicsWindowHelper.GetColorFromString("yellow");
			bool color = GraphicsWindowHelper.InvokeWithReturn(() => ((SolidColorBrush) s.Fill).Color.Equals(expectedColor));
			bool size = GraphicsWindowHelper.InvokeWithReturn(() => s.Height == 11*2);
			Question5Ball.Result = size && color;
		}

		private  void Grade3Timer()
		{
			called = 0;
			Question3Timer.Run();
			Question3Timer.Result = called >= 1 && Timer.GetInterval() == 100;
			Timer.Stop();
		}

		private  void Grade2SetAction1()
		{
			Question2SetAction1.Run();
			Question2SetAction1.Result = (5 == GetAction()[1]);
		}

		private  void Grade4SetAction2()
		{
			Question4SetAction2.Run();
			Question4SetAction2.Result = (-6 == GetAction()[2]);
		}

		private  Primitive GetAction()
		{
			return Question1Move.GetVariable("action");
		}

		private  void Grade1Move()
		{
			called = 0;
			Question1Move.Run();
			Question1Move.Result = called == 1;
		}


		public  void DoMovement()
		{
			called++;
			MovePaddle();
			MoveBall();
		}

		private  void MoveBall()
		{
			var movement = called % 24 < 12 ? -6  : 6;
			ballX += movement;
			ShapeMaker.CenterShapeAt(Question1Move.GetVariable("ball"), ballX, yTop + length / 2);
        }

		private  void MovePaddle()
		{
			var index = called%22 < 12 ? 1:2;
			paddleY = paddleY + GetAction()[index];
			ShapeMaker.CenterShapeAt(paddle, xTop + width, paddleY);
			
		}
	}
	public static  class IntegerExtensions
	{
		public static void Times(this int times, Action<object> action)
		{
			for (int i = 1; i <= times; i++)
			{
				action(i);
			}
		}
	}
}